package me.firstapp.spi.bbs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonMember;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.bbs.MemberService;

@Service
public class MemberServiceImpl extends BaseServiceImpl implements MemberService {

	public SingleObject<JsonMember> register(String email, String name, String password, String code) {
		SingleObject<JsonMember> result = new SingleObject<JsonMember>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("email", email);
			parameters.put("name", name);
			parameters.put("password", password);
			parameters.put("code", code);
			JSONObject resultObject = api(parameters, "/member/register.do", HttpMethod.POST, "用户注册");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject jsonObject = resultObject.getJSONObject("object");
				if (jsonObject != null) {
					JsonMember member = JSONObject.toJavaObject(jsonObject, JsonMember.class);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
					result.setObject(member);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonMember> findMemberById(Long memberId) {
		SingleObject<JsonMember> result = new SingleObject<JsonMember>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			JSONObject resultObject = api(parameters, "/member/findMemberById.do", HttpMethod.GET, "通过用户id查询用户");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject jsonObject = resultObject.getJSONObject("object");
				if (jsonObject != null) {
					JsonMember member = JSONObject.toJavaObject(jsonObject, JsonMember.class);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
					result.setObject(member);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonMember> login(String account, String password) {
		SingleObject<JsonMember> result = new SingleObject<JsonMember>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			String email = "";
			String name = "";
			if (StrUtils.isMail(account)) {
				email = account;
			} else {
				name = account;
			}
			parameters.put("email", email);
			parameters.put("name", name);
			parameters.put("password", password);
			JSONObject resultObject = api(parameters, "/member/login.do", HttpMethod.POST, "用户登录");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject jsonObject = resultObject.getJSONObject("object");
				if (jsonObject != null) {
					JsonMember member = JSONObject.toJavaObject(jsonObject, JsonMember.class);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
					result.setObject(member);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonMember> updateMemberInfo(Long memberId, String avatar, String url, String signature, Integer status) {
		SingleObject<JsonMember> result = new SingleObject<JsonMember>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("avatar", avatar);
			parameters.put("url", url);
			parameters.put("signature", signature);
			parameters.put("status", status);

			JSONObject resultObject = api(parameters, "/member/updateMemberInfo.do", HttpMethod.POST, "更新用户信息");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject jsonObject = resultObject.getJSONObject("object");
				if (jsonObject != null) {
					JsonMember member = JSONObject.toJavaObject(jsonObject, JsonMember.class);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
					result.setObject(member);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public SingleObject<Object> alterLoginPassword(Long memberId, String oldPassword, String newPassword, String code) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("oldPassword", oldPassword);
			parameters.put("newPassword", newPassword);
			parameters.put("code", code);

			JSONObject resultObject = api(parameters, "/member/alterLoginPassword.do", HttpMethod.POST, "更改登录密码");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public PageObject<JsonMember> findMemberPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Long startTime, Long endTime) {
		PageObject<JsonMember> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			parameters.put("startTime", startTime);
			parameters.put("endTime", endTime);
			JSONObject jsonObject = apiBody(parameters, "/member/findMemberPage.do", "分页查询用户", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonMember> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonMember> memberList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonMember member = JSONObject.toJavaObject(_jsonObject, JsonMember.class);
								memberList.add(member);
							}
						}
					}
					jsonPage.setList(memberList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}

		return result;
	}

	@Override
	public SingleObject<Object> resetPassword(String email, String code, String newPassword) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("email", email);
			parameters.put("code", code);
			parameters.put("newPassword", newPassword);
			JSONObject resultObject = api(parameters, "/member/resetPassword.do", HttpMethod.POST, "重置密码");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
